#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include <pthread.h>
#include <time.h>
#include "caml.h"
#include "events.h"
#include "interface.h"
#include "keyboard.h"
#include "display.h"
#include "debug.h"
#include "tools.h"
#include "seek.h"

int			detect_char(p_context			c,
				    int				zone,
				    int				ch,
				    value			ba,
				    video			*vid)
{
  WINDOW		*w = c->win[zone];
  int			*p;
  double		incr;
  char			*path = NULL; /* file selected (mouse) */

  switch (ch)
  {
  case REQ_INFOBAR:
    /* grepme focus = Zone_... */
    c = context_add_window(c, ZONE_INFOBAR, ba);
    return (REDRAW);

  case REQ_PLAYLIST:
    c = context_add_window(c, ZONE_PLAYLIST, ba);
    return (REDRAW);

  case REQ_MENUBAR:
    c = context_add_window(c, ZONE_MENUBAR, ba);
    return (REDRAW);

  case REQ_EXPLORER:
    c = context_add_window(c, ZONE_EXPLORER, ba);
    return (REDRAW);

  case REQ_HELP:
  case REQ_HELP2:
    mvwprintw(w, 0, 2, "HELP");
    /* call help() */
    return (1);

  case REQ_FULLSCREEN:
    full_screen(ba);
    return (REDRAW);

  case KEY_RESIZE:
    /* set_subwin_size(c, ba); */
    clear();
    redraw(ba);
    p[IBA_UPDATE_WVIDEO] = 1;
    return (REDRAW);

  case REQ_PAUSE:
  case REQ_PAUSE2:
    display_text(c, ZONE_VIDEO, 0 + IS_FRAME, 0 + IS_FRAME, "PAUSED");
    wrefresh(WVIDEO);
    wait_pause(c);
    return (1);

  case REQ_QUIT:
    quit(c, 20);
    return (1);

  case REQ_TERM:
    def_prog_mode();
    endwin();

    if (system("/bin/sh") == -1) /* /bin/sh should exists... */
      wprintw(stdscr, 0, 0, "UNABLE TO CALL SH... xD\n");

    reset_prog_mode();
    refresh();
    return (1);

  /* case REQ_FWD: */
  /*   incr = 10; */
  /*   /\* update_progress_bar  *\/ */
  /*   /\* debug_pause(); *\/ */
  /*   do_seek(incr, vid); */
  /*   return (1); */

  /* case REQ_FFWD: */
  /*   incr = 40; */
  /*   do_seek(incr, vid); */
  /*   /\* update_progress_bar  *\/ */
  /*   return (1); */

  /* case REQ_BWD: */
  /*   incr = -10.0; */
  /*   do_seek(incr, vid); */
  /*   /\* update_progress_bar  *\/ */
  /*   return (1); */

  /* case REQ_FBWD: */
  /*   incr = -40.0; */
  /*   do_seek(incr, vid); */
  /*   /\* update_progress_bar  *\/ */
  /*   return (1); */

  case KEY_MOUSE:
    if ((getmouse(&c->mouse) == OK) && (c->mouse.bstate & BUTTON1_CLICKED))
    {/* left mouse button click */
      if ((path = explorer_is_in(c)) != NULL)
      {
	if (WINFOBAR)
	{
	  wclear(WINFOBAR);
	  mvwprintw(WINFOBAR, IS_FRAME, IS_FRAME, "SELECTED\n" /* %s\n", path */);

	  if (IS_FRAME)
	    display_frame(WINFOBAR);

	  touchwin(WMAIN);
	  wrefresh(WINFOBAR);
	  return (1/* ADD_PLAYLIST */);
	}
	/* mvwprintw(stdscr, 0, 0, "SELECTED\n" /\* %s\n", path *\/); */
      }

      if (wenclose(WMENUBAR, c->mouse.y, c->mouse.x))
      {
	display_text(c, ZONE_VIDEO, 0 + IS_FRAME, 0 + IS_FRAME, "PAUSED");
	wrefresh(WVIDEO);
	wait_pause(c);
      }

      return (1);
    }

  default: /* not a "valid" char */
    return (0);
  }
}
